package com.liu.service.impl;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.liu.config.RedisConfig;
import com.liu.dao.ViewDetailDao;
import com.liu.service.intf.ViewDetailService;
import com.liu.utils.JedisUtils;
import com.liu.vo.R;
import entity.Viewdetail;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class ViewDetailServiceImpl implements ViewDetailService {
    @Autowired
    private ViewDetailDao dao;

    @Override
    public R queryViewDetailByVid(int vid) {
        Viewdetail viewdetail;
        if (vid>0){
            if (JedisUtils.getStr(RedisConfig.VIEW_DETAIL+vid)!=null){
                viewdetail = JSON.parseObject(JedisUtils.getStr(RedisConfig.VIEW_DETAIL+vid), entity.Viewdetail.class);
                return R.ok(viewdetail);
            }
            else {
                QueryWrapper<Viewdetail> queryWrapper = new QueryWrapper<>();

                queryWrapper.eq("vid",vid);
                viewdetail = dao.selectOne(queryWrapper);
                JedisUtils.setStr(RedisConfig.VIEW_DETAIL+vid,JSON.toJSONString(viewdetail));
                return R.ok(viewdetail);
            }
        }
        return R.fail("网路错误");
    }
}
